package com.mlz.mlzmall.ui.activity.act.personal;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.AllTagBean;
import com.mlz.mlzmall.bean.AllTagBean.DataBean;
import com.mlz.mlzmall.bean.AllTagBean.DataBean.AgeGroupBean;
import com.mlz.mlzmall.bean.AllTagBean.DataBean.CharacterBean;
import com.mlz.mlzmall.bean.AllTagBean.DataBean.DressStyleBean;
import com.mlz.mlzmall.bean.StatusBean;
import com.mlz.mlzmall.bean.UserBean;
import com.mlz.mlzmall.ui.activity.act.base.TopTitleView;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.RequestUtils;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyBaseActivity;
import com.mlz.mlzmall.ui.adapter.base.MallHolder;
import com.mlz.mlzmall.ui.adapter.base.ShopMallAdapter;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.IntNum;
import com.mlz.mlzmall.ui.utils.util.LogUtils;
import com.ta.utdid2.android.utils.IntUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yhf on 2016/5/25
 */
public class PersonalAttrActivity extends VolleyBaseActivity {

    private static View pw;
    private static ListView listView;
    private static PopupWindow pWindow;

    private TextView tv_age;//显示控件
    private TextView tv_character;
    private TextView tv_style;

    private RadioGroup radiogroup;//选择性别
    private RadioButton rb_girl;
    private RadioButton rb_boy;
    private Button btn_attribute_save;//保存设置
    private String userName;
    private int userSex;
    private int character;
    private int age;
    private int style;
    private List<AgeGroupBean> age_group;
    private List<CharacterBean> character_group;
    private List<DressStyleBean> dressStyle_group;
    private int sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_attribute);//个人属性界面
        initView();
        initData();
        setViewContent();
    }
    private void setViewContent() {
        TopTitleView loginTopTitle = (TopTitleView) findViewById(R.id.login_top_title);
        loginTopTitle.setLeftButtonContent(null, R.mipmap.pic_2_1_btn_back, null);
        loginTopTitle.setTitleButtonContent("个人属性", null, null);
        loginTopTitle.setOnLeftButtonClickListener(new TopTitleView.OnLeftButtonClickListener() {
            @Override
            public void onLeftBtnClick(View button) {
                finish();
                PersonalAttrActivity.this.overridePendingTransition(R.anim.right_in,R.anim.right_out);
            }
        });
    }
    private void initData() {//设置显示数据
        UserBean ub = MyApplication.getmInstance().getLoginUser();
        LogUtils.d(ub.getUserName() + ub.getUserAge() + ub.getUserCharacter() + ub.getUserStyle() + ub.getUserSex());
        userSex = ub.getUserSex();
        character =  ub.getCharacterTagId();
        age =  ub.getAgeGroupTagId();
        style =  ub.getDressStyleTagId();

        tv_age.setText(ub.getUserAge() + "");
        tv_character.setText(ub.getUserCharacter() + "");
        tv_style.setText(ub.getUserStyle() + "");
        if (userSex == 2) {
            rb_girl.setChecked(true);
        }
        if (userSex == 1) {
            rb_boy.setChecked(true);
        }
    }

    private void initView() {
        tv_age = (TextView) findViewById(R.id.tv_age);
        tv_character = (TextView) findViewById(R.id.tv_character);
        tv_style = (TextView) findViewById(R.id.tv_style);
        radiogroup = (RadioGroup) findViewById(R.id.radio_group);
        rb_girl = (RadioButton) findViewById(R.id.rb_girl);
        rb_boy = (RadioButton) findViewById(R.id.rb_boy);
        btn_attribute_save = (Button) findViewById(R.id.btn_attribute_save);
        tv_age.setOnClickListener(this);
        tv_character.setOnClickListener(this);
        tv_style.setOnClickListener(this);
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_boy) {
                    sex = 1;
                } else {
                    sex = 2;
                }
            }
        });
        btn_attribute_save.setOnClickListener(this);
        getAttr();//获取标签
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_age:
                call(1, tv_age);
                break;
            case R.id.tv_character:
                call(2, tv_character);
                break;
            case R.id.tv_style:
                call(3, tv_style);
                break;

            case R.id.btn_attribute_save:
                //将设置上传，返回上层界面
                postAttr();
                break;
        }
    }


    public void call(final int iii, TextView tv) {
        //  退出对话框
        pw = LayoutInflater.from(PersonalAttrActivity.this).inflate(R.layout.dialog_list, null);
        listView = (ListView) pw.findViewById(R.id.listView);
        if (iii == 1) {
            AgeAdapter adapter = new AgeAdapter(this, age_group, R.layout.dialog_list_item);
            listView.setAdapter(adapter);
        }
        if (iii == 2) {
            CharacterAdapter adapter = new CharacterAdapter(this, character_group, R.layout.dialog_list_item);
            listView.setAdapter(adapter);
        }
        if (iii == 3) {
            DressAdapter adapter = new DressAdapter(this, dressStyle_group, R.layout.dialog_list_item);
            listView.setAdapter(adapter);
        }
        pWindow = new PopupWindow(pw, tv.getWidth(), tv.getWidth() * 7 / 9);//弹框显示大小
        pWindow.setFocusable(true);
        pWindow.setBackgroundDrawable(new ColorDrawable(0x55000000));//设置背景为透明
        // pWindow.setAnimationStyle(R.style.AnimationPush);
        //   pWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
        pWindow.showAsDropDown(tv);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (iii == 1) {
                    tv_age.setText(age_group.get(position).getName());
                    age = age_group.get(position).getCustomerTagId();
                }
                if (iii == 2) {
                    tv_character.setText(character_group.get(position).getName());
                    character = character_group.get(position).getCustomerTagId();
                }
                if (iii == 3) {
                    tv_style.setText(dressStyle_group.get(position).getName());
                    style = dressStyle_group.get(position).getCustomerTagId();
                }
                pWindow.dismiss();
            }

        });

    }

    private class AgeAdapter extends ShopMallAdapter<AgeGroupBean> {
        public AgeAdapter(Activity activity, List<AgeGroupBean> age_group, int dialog_list_item) {
            super(activity, age_group, dialog_list_item);
        }

        @Override
        public void convert(MallHolder holder, AgeGroupBean ageGroupBean) {
            holder.setText(R.id.text1, ageGroupBean.getName());
        }
    }

    private class CharacterAdapter extends ShopMallAdapter<CharacterBean> {
        public CharacterAdapter(Activity activity, List<CharacterBean> age_group, int dialog_list_item) {
            super(activity, age_group, dialog_list_item);
        }

        @Override
        public void convert(MallHolder holder, CharacterBean ageGroupBean) {
            holder.setText(R.id.text1, ageGroupBean.getName());
        }
    }

    private class DressAdapter extends ShopMallAdapter<DressStyleBean> {
        public DressAdapter(Activity activity, List<DressStyleBean> age_group, int dialog_list_item) {
            super(activity, age_group, dialog_list_item);
        }

        @Override
        public void convert(MallHolder holder, DressStyleBean ageGroupBean) {
            holder.setText(R.id.text1, ageGroupBean.getName());
        }
    }

    //获取标签
    private void getAttr() {
        tag = "rGet";
        Map<String, String> param1 = new HashMap<String, String>();
        param1.put("telephone", isNull(MyApplication.getInstace().getLoginUser().getAccout()));
        param1.put("deviceId", "");
        param1.put("randomKey", sessionId);

        JSONObject jsonObject = new JSONObject(param1);
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, URL.PUBLIC.GET_CUSTOMER_OPTION, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(tag, jsonObject.toString());
                Gson gson = new Gson();
                AllTagBean userInfo = gson.fromJson(jsonObject.toString(), AllTagBean.class);
                if (userInfo.getStatus() == 0) {
                    DataBean db = userInfo.getData();
                    age_group = db.getAge_group();
                    character_group = db.getCharacter();
                    dressStyle_group = db.getDress_style();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(tag, error.getMessage(), error);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);

    }

    private void postAttr() {
        tag = "rPost";
        Map<String, String> param1 = new HashMap<>();
        param1.put("fullname", isNull(MyApplication.getInstace().getLoginUser().getUserName()));
        param1.put("gender", sex + "");
        param1.put("ageGroupTagId", isNull(age + ""));
        param1.put("characterTagId", isNull(character + ""));
        param1.put("dressStyleTagId", isNull(style + ""));
        LogUtils.d(userSex + "003");
        JSONObject jsonObject = new JSONObject(param1);
        LogUtils.d("we" + jsonObject.toString());
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, URL.CUSTOMER.EDIT_INFO, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                StatusBean sb = gson.fromJson(jsonObject.toString(), StatusBean.class);
                if (sb.getStatus() == 0) {
                    showToast(sb.getMsg());
                    getUserData();
                    PersonalAttrActivity.this.finish();
                    PersonalAttrActivity.this.overridePendingTransition(R.anim.right_in, R.anim.right_out);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(tag, error.getMessage(), error);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag(tag);
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

}
